package com.home.common.framework.core.controller;

import com.home.base.core.framework.msg.PageResponse;
import com.home.base.core.framework.msg.ServerResponse;
import com.home.common.annotation.Log;
import com.home.common.constants.log.LogTypeConstant;
import com.home.common.framework.core.service.BaseService;
import com.home.common.handler.exception.BizException;
import com.home.common.utils.reflex.StringEscapeEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Description: 通用controller
 * Created by 王大宸 on 2020-09-28 19:59
 * Created with IntelliJ IDEA.
 */
public class BaseController<Service extends BaseService<T, PK>, T, PK> {

    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected Service baseService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringEscapeEditor());
        binder.registerCustomEditor(String[].class, new StringEscapeEditor());
    }

    /***
     * 功能说明：列表
     *
     * @author 王大宸
     * @date 2020/9/28 20:28
     * @param
     * @return com.home.common.framework.msg.ServerResponse<java.util.List < T>>
     */
    @GetMapping(value = "/list")
    @ResponseBody
    public ServerResponse<List<T>> list(@RequestParam T entity) throws BizException {
        return ServerResponse.okData(baseService.list(entity));
    }

    /***
     * 功能说明：分页
     *
     * @author 王大宸
     * @date 2020/9/28 20:27
     * @param params limit/page/name
     * @return com.home.common.framework.msg.PageResponse
     */
    @GetMapping(value = "/page")
    @ResponseBody
    public PageResponse<T> page(@RequestParam Map<String, Object> params) throws BizException {
        return baseService.page(params);
    }

    /***
     * 功能说明：新增
     *
     * @author 王大宸
     * @date 2020/9/28 20:27
     * @param entity 需要新增的数据
     * @return com.home.common.framework.msg.ServerResponse<T>
     */
    @PostMapping(value = "")
    @ResponseBody
    @Log(title = "新增", operateType = LogTypeConstant.SAVE)
    public ServerResponse<T> save(@RequestBody @Valid T entity) throws BizException {
        return ServerResponse.okData(baseService.saveInfo(entity));
    }

    /***
     * 功能说明：根据id查询数据
     *
     * @author 王大宸
     * @date 2020/9/28 20:27
     * @param id 需要查询数据的id
     * @return com.home.common.framework.msg.ServerResponse<T>
     */
    @GetMapping(value = "/{id}")
    @ResponseBody
    public ServerResponse<T> getById(@PathVariable("id") PK id) throws BizException {
        return ServerResponse.okData(baseService.getById(id));
    }

    /***
     * 功能说明：修改数据
     *
     * @author 王大宸
     * @date 2020/9/28 20:27
     * @param entity 需要修改的数据
     * @return com.home.common.framework.msg.ServerResponse<T>
     */
    @PutMapping(value = "/{id}")
    @ResponseBody
    @Log(title = "修改", operateType = LogTypeConstant.UPDATE)
    public ServerResponse<T> update(@RequestBody @Valid T entity) throws BizException {
        return ServerResponse.okData(baseService.updateInfo(entity));
    }

    /***
     * 功能说明：删除
     *
     * @author 王大宸
     * @date 2020/9/28 20:26
     * @param id 需要删除数据的id
     * @return com.home.common.framework.msg.ServerResponse<T>
     */
    @DeleteMapping(value = "/{id}")
    @ResponseBody
    @Log(title = "删除", operateType = LogTypeConstant.REMOVE)
    public ServerResponse<T> remove(@PathVariable("id") PK id) throws BizException {
        baseService.remove(id);
        return ServerResponse.ok();
    }

}
